import{Cn as e,bn as t,i as n,wn as r,yn as i}from"./B2uw_jGT1oIxh7.js";var a=new r(`ouiRichtextPresence`);function o(e){let t=0;for(let n=0;n<e.length;n++)t=t*31+e.charCodeAt(n)>>>0;return`hsl(${t%360} 65% 45%)`}function s(e,t){let n;return e.descendants((e,r)=>n?!1:e.isBlock&&e.attrs.bid===t?(n={node:e,pos:r},!1):!0),n}function c(e,t,n){let r=Math.max(0,n),i;return e.descendants((e,n)=>{if(i!=null)return!1;if(e.isText){let a=e.text?.length??0;return r<=a?(i=t+1+n+r,!1):(r-=a,!1)}return e.isLeaf?r<=0?(i=t+1+n,!1):(--r,!1):!0}),i??t+e.nodeSize-1}function l(e,t,n){let r=0,i;return e.descendants((e,a)=>{if(i!=null)return!1;let o=t+1+a;if(e.isText){let t=e.text?.length??0;return n<=o+t?(i=r+Math.max(0,n-o),!1):(r+=t,!1)}return e.isLeaf?n<=o?(i=r,!1):(r+=1,!1):!0}),i??r}function u(e,t){let n=e.resolve(Math.max(0,Math.min(t,e.content.size)));for(let e=n.depth;e>=0;e--){let r=n.node(e);if(r.attrs?.bid&&r.inlineContent){let i=n.before(e);return{bid:r.attrs.bid,offset:l(r,i,t)}}}}function d(e,t){let n=document.createElement(`span`);n.className=`oui-richtext-presence-caret`,n.style.setProperty(`--rt-presence`,t);let r=document.createElement(`span`);return r.className=`oui-richtext-presence-label`,r.textContent=e,n.appendChild(r),n}function f(e,n){let r=[];return n.forEach((t,n)=>{let a=s(e,t.anchor.bid);if(!a)return;let l=t.color??o(t.id),u=t.name??t.id;if(t.anchor.offset==null){r.push(i.node(a.pos,a.pos+a.node.nodeSize,{class:`oui-richtext-presence-block`,style:`--rt-presence: ${l}`,"data-presence":u}));return}let f=c(a.node,a.pos,t.anchor.offset),p=f;if(t.head){let n=t.head.bid===t.anchor.bid?a:s(e,t.head.bid);n&&(p=c(n.node,n.pos,t.head.offset??0))}let m=Math.min(f,p),h=Math.max(f,p);m!==h&&r.push(i.inline(m,h,{class:`oui-richtext-presence-selection`,style:`--rt-presence: ${l}`})),r.push(i.widget(p,()=>d(u,l),{key:`presence-${t.id}-${n}`,side:10}))}),t.create(e,r)}var p=n.create({name:`presence`,addOptions(){return{presence:()=>[]}},addProseMirrorPlugins(){let t=()=>this.options.presence();return[new e({key:a,state:{init:(e,n)=>f(n.doc,t()),apply:(e,n)=>e.docChanged||e.getMeta(a)?f(e.doc,t()):n},props:{decorations(e){return this.getState(e)}}})]}});export{p as Presence,u as anchorForPos,s as findBlockByBid,c as posForTextOffset,o as presenceColor,a as presencePluginKey,l as textOffsetForPos};